function SDOselectShips () {
    // Install checkboxes in front of other ships pilots names
    if (POisPartialRefresh()) {
        var otherShips = window.shipsTab;
    } else {
        var otherShips = document.getElementById ("otherships_content");
    }
    
    // Now let's go through all ships and add a checkbox infront of their names
    // We're skipping the first element because that's the one we have added above
    for (var i=0; i<otherShips.childNodes.length; i++) {
        var td = otherShips.childNodes[i].firstChild.firstChild.lastChild;

        if (td.firstChild.getAttribute ("name") == "report_sighting"){
            td.removeChild (td.firstChild);
        } else {
            var cb = document.createElement ("input");
            cb.type = "checkbox";
            cb.setAttribute ("name", "report_sighting");
            td.insertBefore (cb, td.firstChild);
        }
    }
}

function SDOsubmitManualSighting () {
    var sname = document.getElementById ('sdo_sighting_pilot');
    if (!sname) return;
    sname = sname.value;
    var sector = document.getElementById ('sdo_sighting_sector').value;
    var coords = document.getElementById ('sdo_sighting_coords').value;

    SDOreportSingleSighting (sname, sector, coords);
    SDOmanualSightingReport (); // To close the form
}

function SDOmanualSightingReport () {
    // Open the dialog for manual sighting input
    var div = document.getElementById ("sdo_manual_report_panel");
    if (div) {
        div.parentNode.removeChild(div);
        return;
    }
    var rep = document.getElementById ("sdo_sighting_report");
    if (!rep) return;
    
    // Get the default values: Sector and coordinates
    var sector = POgetSector();
    var coords = POgetCoords();
    
    var div = document.createElement ("div");
    div.id = "sdo_manual_report_panel";
    div.innerHTML = "<table style='margin: 0pt 18px;'><tbody>\
                    <tr><td>Pilot\n<input type='text' id='sdo_sighting_pilot' size='20'></input></td></tr>\
                    <tr><td>Sector\n<input type='text' id='sdo_sighting_sector' size='20' value='"+sector+"'></input></td></tr>\
                    <tr><td>Coords\n<input type='text' id='sdo_sighting_coords' size='20' value='"+coords+"'></input></td></tr>\
                    <tr><td><input type='button' id='sdo_sighting_submit' value='Submit' onclick='SDOsubmitManualSighting();'></input> <input type='button' id='sdo_sighting_cancel' value='Cancel' onclick=\"document.getElementById ('sdo_sighting_report').removeChild(document.getElementById ('sdo_manual_report_panel'));\"></input></td></tr>\
                    </tbody></table>";
    rep.appendChild (div);
}

function SDOreportAlert () {
    // Open the dialog for manual sighting input
    var div = document.getElementById ("sdo_manual_report_panel");
    if (div) {
        div.parentNode.removeChild(div);
        return;
    }
    var rep = document.getElementById ("sdo_sighting_report");
    if (!rep) return;
    
    // Get the default values: Sector and coordinates
    var sector = POgetSector();
    var coords = POgetCoords();
    
    var div = document.createElement ("div");
    div.id = "sdo_manual_report_panel";
    div.innerHTML = "<table style='margin: 0pt 18px;'><tbody>\
                    <tr><td>Type<br><select type='select' id='sdo_alert_type' size='1'><option po_id=1>Intel</option><option po_id=3>Combat</option><option po_id=4>Hack</option><option po_id=5>Informational</option><option po_id=6>Other</option></td></tr>\
                    <tr><td>Sector<br><input type='text' id='sdo_alert_sector' size='20' value='"+sector+"'></input></td></tr>\
                    <tr><td>Coords<br><input type='text' id='sdo_alert_coords' size='20' value='"+coords+"'></input></td></tr>\
                    <tr><td>Header<br><input type='text' id='sdo_alert_header' size='20'></input></td></tr>\
                    <tr><td>Details<br><textarea type='text' id='sdo_alert_details' rows='3' style='width: 95%'></textarea></td></tr>\
                    <tr><td><input type='button' id='sdo_alert_submit' value='Submit' onclick='SDOsubmitManualAlert();'></input> <input type='button' id='sdo_alert_cancel' value='Cancel' onclick=\"document.getElementById ('sdo_sighting_report').removeChild(document.getElementById ('sdo_manual_report_panel'));\"></input></td></tr>\
                    </tbody></table>";
    rep.appendChild (div);
    document.getElementById ('sdo_alert_type').selectedIndex = 0;
}

function SDOsubmitManualAlert () {
    var div = document.getElementById ("sdo_manual_report_panel");
    if (!div) {
        return;
    }
    var sel = document.getElementById ('sdo_alert_type');
    var url = POgetServerUrl("alerter/save_manual_alert.php", false);
    var type = sel.options[sel.selectedIndex].getAttribute("po_id");
    var txt = document.getElementById ("sdo_alert_header").value.replace (/&/g, "and");
    if (type == "1") {
        txt = "Intel:"+txt;
    }
    var query = "atype="+type
        + "&asector="+document.getElementById ("sdo_alert_sector").value.replace (/&/g, "and")
        + "&acoords="+document.getElementById ("sdo_alert_coords").value.replace (/&/g, "and")
        + "&aheader="+txt
        + "&adetails="+document.getElementById ("sdo_alert_details").value.replace (/&/g, "and")
        + "&universe="+POgetUniverse ()
        + "&userid="+POgetUserID ()
        + "&uname="+POgetUserName ()
        + "&vcode="+POgetValidationCode ();
    POajaxPost (url, "POST", query, null)
    SDOreportAlert ();
}

function SDOreportSafe () {
    // Open the dialog for manual sighting input
    var div = document.getElementById ("sdo_manual_report_panel");
    if (div) {
        div.parentNode.removeChild(div);
        return;
    }
    var rep = document.getElementById ("sdo_sighting_report");
    if (!rep) return;
    
    // Get the default values: Sector and coordinates
    var sector = POgetSector();
    var coords = POgetCoords();
    
    var div = document.createElement ("div");
    div.id = "sdo_manual_report_panel";
    div.innerHTML = "<table style='margin: 0pt 18px;'><tbody>\
                    <tr><td>Sector<br><input type='text' id='sdo_safe_sector' size='20' value='"+sector+"'></input></td></tr>\
                    <tr><td>Coords<br><input type='text' id='sdo_safe_coords' size='20' value='"+coords+"'></input></td></tr>\
                    <tr><td>Comment<br><input type='text' id='sdo_safe_comment' size='20'></input></td></tr>\
                    <tr><td><input type='button' id='sdo_safe_submit' value='Submit' onclick='SDOsubmitSafe();'></input> <input type='button' id='sdo_safe_cancel' value='Cancel' onclick=\"document.getElementById ('sdo_sighting_report').removeChild(document.getElementById ('sdo_manual_report_panel'));\"></input></td></tr>\
                    </tbody></table>";
    rep.appendChild (div);
}

function SDOsubmitSafe () {
    var div = document.getElementById ("sdo_manual_report_panel");
    if (!div) {
        return;
    }
    var sel = document.getElementById ('sdo_alert_type');
    var url = POgetServerUrl("alerter/save_safe_tile.php", false);
    var query = "sector="+document.getElementById ("sdo_safe_sector").value.replace (/&/g, "and")
        + "&coords="+document.getElementById ("sdo_safe_coords").value.replace (/&/g, "and")
        + "&comment="+document.getElementById ("sdo_safe_comment").value.replace (/&/g, "and")
        + "&universe="+POgetUniverse ()
        + "&userid="+POgetUserID ()
        + "&uname="+POgetUserName ()
        + "&vcode="+POgetValidationCode ();
    POajaxPost (url, "POST", query, null)
    SDOreportSafe ();
}

function SDOreportSighting() {
    // Report the ships seen
    var ajax = POisPartialRefresh();
    if (ajax) {
        var ships = window.shipsTab;
    } else {
        var ships = document.getElementById ("otherships_content");
    }
    if (!ships) return;
    
    // Get the default values: Sector and coordinates
    var sector = POgetSector();
    var coords = POgetCoords();
    
    // Loop over the ships in the OtherShips windos, check which ones are checked and send them in a sighting report
    var cbs = ships.getElementsByTagName ("input");
    var reported = false;
    for (var i=0; i<cbs.length; i++) {
        var cb = cbs[i];
        if (cb.getAttribute ("name") != "report_sighting") continue;
        if (!cb.checked) continue;
        
        // OK, here we have a checked checkbox. Let's gather the pilot info and make the sighting report.
        if (ajax) {
            var pilotID = cb.nextSibling.href.split ("(")[1].split(",")[0];
        } else {
            var pilotID = cb.nextSibling.href.split ("=")[1].split("&")[0];
        }
        var pilotName = cb.nextSibling.textContent;
        
        SDOreportSingleSighting (pilotName, sector, coords);
        
        reported = true;
    }
    
    if (reported) {
        SDOselectShips(); // To remove the checkboxes
    } else {
        SDOmanualSightingReport ();
    } 
}

function SDOreportSingleSighting (pilotName, sector, coords) {
    var url = POgetServerUrl("alerter/savesighting.php", false);
    var query = "sname="+pilotName
        + "&ssector="+sector.replace (/&/g, "and")
        + "&scoords="+coords.replace (/&/g, "and")
        + "&universe="+POgetUniverse ()
        + "&userid="+POgetUserID ()
        + "&uname="+POgetUserName ()
        + "&vcode="+POgetValidationCode ();
    POajaxPost (url, "POST", query, null)
}

function SDOinstallReportingPanel () {
    // Add the control panel for sighting reporting.
    var ships = document.getElementById ("otherships");
    if (!ships) return;
    
    // Make sure it's always visible
    ships.style.display = "block";

    // Create a new row below the OtherShips label
    var otherships = ships.firstChild.childNodes[1].firstChild;
    var tr = document.createElement ("tr");
    ships.firstChild.insertBefore (tr, ships.firstChild.childNodes[1]);
    var newships = otherships.cloneNode (false);
    tr.appendChild (newships);
    
    // Add the control panel into the new row
    newships.innerHTML = "<span style='margin: 0pt 18px;' id='sdo_sighting_report'>\
                            <span><a href='javascript:SDOselectShips();void(0);'>Select</a></span>\
                            <span>|</span>\
                            <span><a href='javascript:SDOreportSighting();void(0);'>Report</a></span>\
                            <span>|</span>\
                            <span><a href='javascript:SDOreportAlert();void(0);'>Alert</a></span>\
                            <span>|</span>\
                            <span><a href='javascript:SDOreportSafe();void(0);'>Safe</a></span>\
                         </span>";
}

// Make sure it will work with PartialRefresh
addUserFunction (function(){window.shipsTabExterior.style.display = "block";});
SDOinstallReportingPanel ();